window.$ = WLJQ;
var busyIndicator = null;
var dailyThinkingStorage = null;

function wlCommonInit() {
	busyIndicator = new WL.BusyIndicator('content');
	loadAllDailyThinking();
}

function invokeGetLoginName() {
	alert(WL.Client.getLoginName("DailyThinkingLoginRealm"));
}

function loadAllDailyThinking() {
	//busyIndicator.show();
	var invocationData = {
		adapter : 'DailyThinkingDao',
		procedure : 'queryAll'
	};
	var options = {
		onSuccess : loadAllDailyThinkingCallback,
		onFailure : loadAllDailyThinkingCallback
	};
	WL.Client.invokeProcedure(invocationData, options);

}

function loadAllDailyThinkingCallback(response) {
	busyIndicator.hide();
	dailyThinkingStorage = [];
	if (!response.invocationResult.isSuccessful) {
		handleError(response.invocationResult.errors[0]);
	} else {
		
		dailyThinkingStorage = response.invocationResult.resultSet;
		showDailyThinking();
	}
}

function showDailyThinking() {
	$("#dailyThinkingList").hide;
	$.each(dailyThinkingStorage, function(i, item) {
		var li = $("<li/>").height(60);
		var image = $("<img/>").attr("src", "images/i-icon-6.png").attr(
				"align", "left");
		var nameSpan = $("<span/>").html(item.user_name);
		var action = $("<p/>").html(item.action);
		var challenge = $("<p/>").html(item.challenge);
		li.append(image).append(nameSpan).append("<br/>").append(action).append(challenge).append("<br/><br/>");
		$("#dailyThinkingList").append(li);
		
	});
	$("#dailyThinkingList").slideDown(1000);
}

function handleError(errorMsg) {
	WL.SimpleDialog.show("每日一思", "Adapter Error" + errorMsg, [ {
		text : "重试",
		handler : loadAllDailyThinking
	} ]);
}

function doLogout() {
	WL.Logger.debug("APP: doLogout()");
	WL.Client.logout('DailyThinkingLoginRealm');
	WL.Client.reloadApp();
}